/*  Load XML Doc
    
    this will load and manipulate the XML files

    XPath:
    // this reads all the name attributes of the column
    var path = "/tables/table/column/@name";
    
    // gets the columns for a table
    var path = "/tables/table[@name='Contact']/column/@name";

*/

/**
@function loadXMLDoc reads in an xml file depending on browser type
@param {string} dname - full path and name to xml file
@returns {xhttp} valid respose object
*/
function loadXMLDoc(dname) {
    if (window.XMLHttpRequest) {
        xhttp=new XMLHttpRequest();
    }
    else {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    xhttp.open("GET",dname, false);
    xhttp.send();

    return xhttp.responseXML;
}

/**
@function xmlGetColumns gets all of the columns for a table
@param {string} tableName - the name of the table for selecing columns
@returns {Array} - a simple array of column names as strings
*/
function xmlGetColumns_old(tableName) {
    var xml = loadXMLDoc("cmr.xml");
    var path = "/tables/table[@name='" + tableName + "']/column/@name";
    var r = [];

    // IE... ?
    if (window.ActiveXObject) {
        var nodes = xml.selectNodes(path);
        var result = nodes.iterateNext();
        for (i = 0; i < nodes.length; i++) {
            //document.write(nodes[i].childNodes[0].nodeValue);
            //document.write("<br>");
        }
    }
    else if (document.implementation && document.implementation.createDocument) {
        var nodes = xml.evaluate(path, xml, null, 0, null);
        var result = nodes.iterateNext();
        while (result) {
            //document.write(result.childNodes[0].nodeValue.toLowerCase());
            //document.write("<br>");
            var c = result.childNodes[0].nodeValue.toLowerCase();
            r.push(c);
            result = nodes.iterateNext();
        }
    }
    // an array of column names only...
    return r;

} // getColumns()

/**
@function xmlGetColumns gets all of the columns for a table
@param {string} tableName - the name of the table for selecing columns
@returns {Array} - an array of column objects
*/
function xmlGetColumns(tableName, x, y) {
    var xml = loadXMLDoc("cmr.xml");
    var path = "/tables/table[@name='" + tableName + "']/column";
    var ret = [];
    // this.x + 2, startY + (i * 13)
    var startX = x;
    var startY = y + 26;
    var i = 0;

    // IE... ?
    if (window.ActiveXObject) {
        //
    }
    else {
        var nodes = xml.evaluate(path, xml, null, 0, null);
        var result = nodes.iterateNext();

        while (result) {
            // loop al of the nodes...
            var attr = result.attributes;
            // create the col object and load it with data
            var col = new erdColumn(attr.item(0).nodeValue, /* id */
                                    attr.item(1).nodeValue, /* name */
                                    attr.item(2).nodeValue, /* type */
                                    attr.item(3).nodeValue, /* visible */
                                    startX, startY + (i * 13)); // pos inited to zero

            // push the col into the array and get the next node
            ret.push(col);
            result = nodes.iterateNext();
            i++;
        }
    }

    // an array of column objects
    return ret;
}
